package triannon.client.components;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Paint;
import java.awt.Toolkit;

import javax.swing.Box;
import javax.swing.JPanel;

import triannon.client.selection.Selection;
import triannon.client.selection.SelectionEvent;
import triannon.client.selection.SelectionListener;
import triannon.client.selection.Selection.SelectedObject;

@SuppressWarnings("serial")
public class Menu extends JPanel implements SelectionListener {

	public static final int MENU_WIDTH = 250;

	private Paint paint;
	private MiniMap miniMap;
	private JPanel buttonPanel;

	public Menu(MiniMap miniMap) {
		this.miniMap = miniMap;

		init();
	}

	private void init() {
		setLayout(new GridBagLayout());
		GridBagConstraints constraints = new GridBagConstraints();
		constraints.gridx = 0;
		constraints.gridy = 0;
		constraints.anchor = GridBagConstraints.NORTH;
		constraints.insets = new Insets(5, 5, 5, 5);

		add(miniMap, constraints);

		constraints.gridy++;
		constraints.weightx = 1.0d;
		constraints.weighty = 1.0d;
		constraints.fill = GridBagConstraints.BOTH;
		add(Box.createGlue(), constraints);

		constraints.gridy++;
		constraints.weighty = 0.0d;
		constraints.anchor = GridBagConstraints.SOUTH;

		buttonPanel = new JPanel();
		buttonPanel.setOpaque(false);
		buttonPanel.setLayout(new GridBagLayout());
		buildButtonPanel(null);
		add(buttonPanel, constraints);

		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();

		final Color BORDER_COLOR_1 = new Color(250, 250, 250, 50);
		final Color BORDER_COLOR_2 = new Color(0, 0, 0, 100);

		paint = new GradientPaint(d.width / 2, 0.0f, BORDER_COLOR_1, d.width / 2f,
				d.height, BORDER_COLOR_2);

		Selection.addSelectionListener(this);
	}

	private void buildButtonPanel(SelectedObject object) {
		buttonPanel.removeAll();
		GridBagConstraints constraints = new GridBagConstraints();
		constraints.fill = GridBagConstraints.HORIZONTAL;
		constraints.gridx = 0;
		constraints.gridy = 0;
		constraints.weightx = 1.0d;
		constraints.weighty = 1.0d;
		constraints.anchor = GridBagConstraints.CENTER;
		constraints.insets = new Insets(5, 5, 5, 5);

		if (object == SelectedObject.Unit) {
			buttonPanel.add(new TButton("Move"), constraints);

			constraints.gridx++;

			buttonPanel.add(new TButton("Stop"), constraints);

			constraints.gridx = 0;
			constraints.gridy++;

			buttonPanel.add(new TButton("Hold"), constraints);

			constraints.gridx++;

			buttonPanel.add(new TButton("Attack"), constraints);
		} else if (object == SelectedObject.Building) {
			buttonPanel.add(new TButton("Build Some Stuff"), constraints);
		}

		validate();

		repaint();
	}

	@Override
	public void selectionChanged(SelectionEvent evt) {
		if (evt.getSelectedSprites().size() > 0) {
			buildButtonPanel(evt.getSelectedObject());
		} else {
			buildButtonPanel(null);
		}
	}

	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);

		Graphics2D g2d = (Graphics2D) g;
		g2d.setPaint(paint);
		g2d.setPaintMode();

		g2d.fillRect(0, 0, getWidth(), getHeight());
	}

	@Override
	public Dimension getMinimumSize() {
		return getPreferredSize();
	}

	@Override
	public Dimension getMaximumSize() {
		return getPreferredSize();
	}

	@Override
	public Dimension getPreferredSize() {
		return new Dimension(MENU_WIDTH, super.getPreferredSize().height);
	}
}
